package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@Configuration
//@EnableWebMvc
@Slf4j
public class MyWebConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addWebRequestInterceptor(new MyWebRequestInterceptor1()).addPathPatterns("/**");
        registry.addInterceptor(new MyHandlerInterceptor1()).addPathPatterns("/**");
        registry.addInterceptor(new MyHandlerInterceptor2()).addPathPatterns("/**");
    }

    private class MyHandlerInterceptor1 implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            log.info("MyHandlerInterceptor1... preHandle");
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            log.info("MyHandlerInterceptor1... postHandle");
        }
    }

    private class MyHandlerInterceptor2 implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            log.info("MyHandlerInterceptor2... preHandle");
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            log.info("MyHandlerInterceptor2... postHandle");
        }
    }

    private class MyWebRequestInterceptor1 implements WebRequestInterceptor{

        @Override
        public void preHandle(WebRequest request) throws Exception {
            log.info("MyWebRequestInterceptor1... preHandle");
        }

        @Override
        public void postHandle(WebRequest request, ModelMap model) throws Exception {
            log.info("MyWebRequestInterceptor1... postHandle");
        }

        @Override
        public void afterCompletion(WebRequest request, Exception ex) throws Exception {
            log.info("MyWebRequestInterceptor1... afterCompletion");
        }
    }

   // @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean registrationBean =  new ServletRegistrationBean(new MyAsyncServlet(),"/");
        registrationBean.setName("MyAsyncServlet");
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
    }

    private class MyAsyncServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            log.info("MyAsyncServlet...doGet");
        }
    }

}
